package com.sbs.gaoxinqutoilet.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.just.agentweb.AgentWeb;
import com.sbs.gaoxinqutoilet.R;
import com.sbs.gaoxinqutoilet.base.BaseBackFragment;
import com.sbs.gaoxinqutoilet.http.HttpApis;
import com.sbs.gaoxinqutoilet.interfaces.Constants;
import com.sbs.gaoxinqutoilet.utils.SPUtil;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 聪明一只哈 on 2020/4/26.
 * Time: 2020/4/26  10:08
 */
public class NewsContentFragment_webview extends BaseBackFragment {

    public static String NEWS_Key = "new_content_id";

    Unbinder unbinder;
    @BindView(R.id.toolbar_back)
    ImageView toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_iv_add)
    ImageView toolbarIvAdd;
    @BindView(R.id.toolbar_iv_menu)
    ImageView toolbarIvMenu;
    @BindView(R.id.toolbar_tv_menu)
    TextView toolbarTvMenu;
    @BindView(R.id.toolbar_rl_menu)
    AutoRelativeLayout toolbarRlMenu;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.ll_webview_content)
    WebView llWebviewContent;
    /* @BindView(R.id.webview_news)
     WebView webviewNews;*/
    private AgentWeb agentWeb;
    private String newsID;

    public static NewsContentFragment_webview newInstance(String newsId) {
        Bundle args = new Bundle();
        args.putSerializable(NEWS_Key, newsId);
        NewsContentFragment_webview fragment = new NewsContentFragment_webview();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_newscontent_webview, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView(inflater);
        initData();
        initListener();
        return attachToSwipeBack(view);
    }


    private void initView(LayoutInflater inflater) {

        //设置回键
        initToolbarBack(toolbar);
        toolbarTitle.setText("资讯");
        //返回按钮
        toolbarBack.setVisibility(View.VISIBLE);


        //获取传递过来的值
        Bundle arguments = getArguments();
        if (arguments != null) {
            newsID = arguments.getString(NEWS_Key);
        }
        String token = SPUtil.getString(Constants.KEY_TOKEN, "");
        if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(newsID)) {//"1410017d49bb-fb20-45af-82c2-e7656965927e"
            String url = HttpApis.newContent_detial + newsID + "?token=" + token;



        //webview_game.loadUrl("http://wxdt.jurentech.xin/hjkg/index.html?id="+app_only_id);
        llWebviewContent.loadUrl(url);

        //声明WebSettings子类
        WebSettings webSettings = llWebviewContent.getSettings();
        //设置自适应屏幕，两者合用
        // webview_game.setInitialScale(1);//这个很重要,但是在游戏界面,需要注释掉
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(false); // 缩放至屏幕的大小  但是在游戏界面,需要false
        //缩放操作
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(false); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件
        webSettings.setAllowContentAccess(true);
        //支持通过JS打开新窗口
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        //设置支持js交互  对这个游戏界面很重要
        webSettings.setJavaScriptEnabled(true);

        webSettings.setSaveFormData(true);

        //优先使用缓存:
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        //缓存模式如下：
        //LOAD_CACHE_ONLY: 不使用网络，只读取本地缓存数据
        //LOAD_DEFAULT: （默认）根据cache-control决定是否从网络上取数据。
        //LOAD_NO_CACHE: 不使用缓存，只从网络获取数据.
        //LOAD_CACHE_ELSE_NETWORK，只要本地有，无论是否过期，或者no-cache，都使用缓存中的数据。

            llWebviewContent.setLayerType(View.LAYER_TYPE_SOFTWARE, null);


            llWebviewContent.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                /*view.loadUrl(url);
                return true;*/

                //这种方法解决重定向问题   按返回键,不起作用,死循环
                WebView.HitTestResult hitTestResult = view.getHitTestResult();
                //hitTestResult==null解决重定向问题
                if (!TextUtils.isEmpty(url) && hitTestResult == null) {
                    view.loadUrl(url);
                    return true;
                }
                //WebBackForwardList mWebBackForwardList = webview.copyBackForwardList();//获取栈内存在的URL
                //for (int i = 0, p = mWebBackForwardList.getSize(); i < p; i++) {
                //  Log.e("====", "" + mWebBackForwardList.getItemAtIndex(i).getUrl());
                //}
                return false;
            }


        });

        }


    }

    private void initData() {

    }

    private void initListener() {

    }


//    @Override
//    public boolean onBackPressedSupport() {
//
//
////        WebView webView = agentWeb.getWebCreator().getWebView();
////        if (webView.canGoBack()) {
////            webView.goBack();
////            return true;
////        } else {
////            return super.onBackPressedSupport();
////        }
//
//
//    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        unbinder.unbind();
        //EventBus.getDefault().unregister(this);
        OkHttpUtils.getInstance().cancelTag(this);
        cancelLoadingDialog();

    }
}
